package controller.Supper;

import pojo.All;
import pojo.Orderse;
import service.Supper.SuperCosOrderService;
import service.Supper.impl.SuperCosOrderServiceImpl;
import util.PageSupport;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "SupCosOrderServlet",urlPatterns = "/orderServlet")
public class SupCosOrderServlet extends HttpServlet {
    Long selId;
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String action = request.getParameter("action");
        SuperCosOrderService scos = new SuperCosOrderServiceImpl();
        int pageSize = 5;
        if(action!=null &&"ordQueryBySel".equals(action)){  //进入订单信息查询
            if (request.getParameter("selId") != null) {
                this.selId = Long.valueOf(request.getParameter("selId"));
            }
            int currentPage = Integer.valueOf(request.getParameter("pageNumber"));   //当前页数
            int totalCount = scos.orderseCount(selId);  //得到总数据行
            //存放Page
            PageSupport pageSupport = new PageSupport();
            pageSupport.setPageSize(pageSize);
            pageSupport.setCurrentPage(currentPage);
            pageSupport.setTotalCount(totalCount);
            pageSupport.setTotalPages(pageSupport.getTotalPages());
            List<All> alls = scos.getOrderseList(currentPage,pageSize,selId);
            pageSupport.setList(alls);
            request.getSession().setAttribute("Pageorder",pageSupport);
            request.getSession().setAttribute("servletURL","orderServlet?action=ordQueryBySel");
            response.sendRedirect("/Seller/ordQuery.jsp");
           // request.getRequestDispatcher("/Seller/ordQuery.jsp").forward(request,response);
        }else if("ordSel".equals(action)){   //确认发货
            Long id = Long.valueOf(request.getParameter("id"));
            int orderseStatus = Integer.valueOf(request.getParameter("orderseStatus"));
            if(orderseStatus==0){
                int n = scos.updateOrderseStatus(Integer.valueOf("1"),id);
                if(n>0){
                    out.print("<script>");
                    out.print("alert('发货成功！');");
                    out.print("location.href='/orderServlet?action=ordQueryBySel&pageNumber=1&selId='+"+selId+";");
                    out.print("</script>");
                }else{
                    out.print("<script>");
                    out.print("alert('发货失败！');");
                    out.print("location.href='/orderServlet?action=ordQueryBySel&pageNumber=1&selId='+"+selId+";");
                    out.print("</script>");
                }
            }else{
                out.print("<script>");
                out.print("alert('抱歉，只有在下单状态才可以发货！！！');");
                out.print("location.href='/orderServlet?action=ordQueryBySel&pageNumber=1&selId='+"+selId+";");
                out.print("</script>");
            }

        }else if("UpdateById".equals(action)){   //进入修改
            Long id = Long.valueOf(request.getParameter("id"));
            All all = scos.getOrderseById(id);
            request.setAttribute("Ord", all);
            request.getRequestDispatcher(request.getContextPath() + "/Seller/ordUpdate.jsp").forward(request, response);
        }else if("ordUpdate".equals(action)){  //执行修改
            String id=request.getParameter("id");
            String orderseAddress=request.getParameter("orderseAddress");
            String orderseMoney=request.getParameter("orderseMoney");
            String orderseStutas=request.getParameter("orderseStatus");
            Orderse orderse=new Orderse();
            orderse.setId(Integer.parseInt(id));
            orderse.setOrderseAddress(orderseAddress);
            orderse.setOrderseMoney(Double.parseDouble(orderseMoney));
            orderse.setOrderseStatus(Integer.parseInt(orderseStutas));
            int n=scos.updateOrderse(orderse);
            if(n>0) {
                out.print("<script>");
                out.print("alert('修改成功！');");
                out.print("location.href='/orderServlet?action=ordQueryBySel&pageNumber=1&selId='+"+selId+";");
                out.print("</script>");
            }else{
                out.print("<script>");
                out.print("alert('修改失败！');");
              //  out.print("location.href='/orderServlet?action=ordQueryBySel&pageNumber=1&selId='+"+selId+";");
                out.print("</script>");

            }
        }
    }
}
